package com.nbjtjc.safe.api.emg;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.service.emg.EmgPlanPublicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 发布的应急预案
 */
@RestController
@RequestMapping("/planPublic")
@CrossOrigin("*")
@Api("应急预案的发布")
public class EmgPlanPublicController extends AbstractController<EmgPlanPublic> {
    @Autowired
    private EmgPlanPublicService emgPlanPublicService;


    @Override
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @SysLog("删除已经发布的应急响应预案")
    public Result delete(int id) {
        Result result = new Result();
        try {
            emgPlanPublicService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @SysLog("查询发布的应急响应")
    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(EmgPlanPublic emgPlanPublic, Page page) {
        Result result = new Result();
        try {
            result = emgPlanPublicService.find(emgPlanPublic, page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @SysLog("查询母公司发布的应急响应")
    @RequestMapping(value = "/findMotherCompanyEmg", method = {RequestMethod.POST,RequestMethod.GET})
    public Result findMotherCompanyEmg(EmgPlanPublic emgPlanPublic, Page page) {
        Result result = new Result();
        try {
            result = emgPlanPublicService.findMotherCompanyEmg(emgPlanPublic, page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @SysLog("发布的应急响应预案")
    public Result add(EmgPlanPublic emgPlanPublic) {
        Result result = new Result();
        try {
            result = emgPlanPublicService.add(emgPlanPublic,false);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @SysLog("调整发布应急响应预案")
    @PostMapping(value = "/republicEmgPlan")
    public Result republicEmgPlan(EmgPlanPublic emgPlanPublic,@RequestParam Map<String,Object> params) throws Exception {
        Result result=new Result();
        result=emgPlanPublicService.republicEmgPlan(emgPlanPublic,params);
        return result;
    }

    @RequestMapping(value = "/addJson", method = {RequestMethod.POST})
    @SysLog("发布的应急响应预案")
    public Result addJSon(@RequestBody EmgPlanPublic emgPlanPublic) {
        Result result = new Result();
        try {
            result = emgPlanPublicService.add(emgPlanPublic,false);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }



    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @SysLog("更新的应急响应预案")
    public Result update(EmgPlanPublic emgPlanPublic) {
        Result result = new Result();
        try {
            result = emgPlanPublicService.update(emgPlanPublic);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @PostMapping("/relievePlanPublic")
    @ApiOperation("解除应急，并发布通知")
    public Result relievePlanPublic(EmgPlanPublic emgPlanPublic, SysNotice sysNotice){
        Result result = new Result();
        try {
            result=emgPlanPublicService.relievePlanPublic(emgPlanPublic,sysNotice);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    /**
     * 获取应急响应的其他信息，子公司的响应信息，值班信息
     * @param emgPlanPublicId
     * @return
     */
    @GetMapping("/getEmgPlanInfo/{emgPlanPublicId}")
    public Result getEmgPlanInfo(@PathVariable("emgPlanPublicId") int emgPlanPublicId){
        Result result = new Result();
        result.setData(emgPlanPublicService.getEmgPlanInfo(emgPlanPublicId));
        return result;
    }
}
